package fox.framework.rpc.context;

import fox.framework.rpc.client.RpcRegister;
import fox.framework.rpc.client.invocation.ConsumeInvoker;
import fox.framework.rpc.client.message.RpcClientRequestInterceptor;
import fox.framework.rpc.client.node.rule.RuleFactory;
import fox.framework.rpc.context.event.RpcEventListener;
import fox.framework.rpc.context.processor.RpcDefinitionRegisterProcessor;
import fox.framework.rpc.context.processor.RpcApplicationProcessor;
import fox.framework.rpc.converter.RpcMessageConverter;
import fox.framework.rpc.server.dispatcher.invoke.ProvideInvoker;

import java.util.List;
import java.util.Set;

/**
 * @author cuichao
 * @Description: RpcApplicationConfigureAdapter
 * @Date: create in 2021/2/2 20:20
 */
public class RpcApplicationConfigureAdapter implements RpcApplicationConfigurer {

    private DefaultRpcApplicationConfigurer configurer = new DefaultRpcApplicationConfigurer();


    @Override
    public String getApplicationName() {
        return configurer.getApplicationName();
    }

    @Override
    public int getPort() {
        return configurer.getPort();
    }

    @Override
    public String getBind() {
        return configurer.getBind();
    }

    @Override
    public String getGlobalProtocol() {
        return configurer.getGlobalProtocol();
    }

    @Override
    public String getGlobalConverterName() {
        return configurer.getGlobalConverterName();
    }

    @Override
    public String getGlobalGroup() {
        return configurer.getGlobalGroup();
    }

    @Override
    public RpcRegister getRegister() {
        return configurer.getRegister();
    }

    @Override
    public RuleFactory getGlobalRuleFactory() {
        return configurer.getGlobalRuleFactory();
    }

    @Override
    public List<RpcMessageConverter> getRpcMessageConverter() {
        List<RpcMessageConverter> converters = addRpcMessageConverter();
        if (converters != null && !converters.isEmpty()) {
            List<RpcMessageConverter> converter = configurer.getRpcMessageConverter();
            converter.addAll(converters);
        }
        return configurer.getRpcMessageConverter();
    }

    @Override
    public List<RpcClientRequestInterceptor> getClientRequestInterceptors() {
        List<RpcClientRequestInterceptor> interceptors = addClientRequestInterceptors();
        if (interceptors != null && !interceptors.isEmpty()) {
            List<RpcClientRequestInterceptor> requestInterceptors = configurer.getClientRequestInterceptors();
            requestInterceptors.addAll(interceptors);
        }
        return configurer.getClientRequestInterceptors();
    }

    @Override
    public List<ProvideInvoker> getProvideInvokers() {
        List<ProvideInvoker> invokers = addProvideInvokers();
        if (invokers != null && !invokers.isEmpty()) {
            List<ProvideInvoker> provideInvokers = configurer.getProvideInvokers();
            provideInvokers.addAll(invokers);
        }
        return configurer.getProvideInvokers();
    }

    @Override
    public List<ConsumeInvoker> getConsumeInvokers() {
        List<ConsumeInvoker> invokers = addConsumeInvokers();
        if (invokers != null && !invokers.isEmpty()) {
            List<ConsumeInvoker> consumeInvokers = configurer.getConsumeInvokers();
            consumeInvokers.addAll(invokers);
        }
        return configurer.getConsumeInvokers();
    }

    @Override
    public List<RpcApplicationProcessor> getApplicationProcessors() {
        configurer.getApplicationProcessors().add(addDefinitionRegisterProcessor());

        List<RpcApplicationProcessor> processors = addRpcApplicationProcessor();
        if (processors != null && !processors.isEmpty()) {
            configurer.getApplicationProcessors().addAll(processors);
        }
        return configurer.getApplicationProcessors();
    }

    @Override
    public Set<RpcEventListener> getRpcEventListeners() {
        Set<RpcEventListener> listeners = addRpcEventListeners();
        if (listeners != null && !listeners.isEmpty()) {
            configurer.getRpcEventListeners().addAll(listeners);
        }
        return configurer.getRpcEventListeners();
    }

    protected List<RpcApplicationProcessor> addRpcApplicationProcessor() {
        return null;
    }

    protected List<RpcMessageConverter> addRpcMessageConverter() {
        return null;
    }


    protected List<RpcClientRequestInterceptor> addClientRequestInterceptors() {
        return null;
    }

    protected List<ProvideInvoker> addProvideInvokers() {
        return null;
    }

    protected List<ConsumeInvoker> addConsumeInvokers() {
        return null;
    }

    protected Set<RpcEventListener> addRpcEventListeners() {
        return null;
    }

    protected RpcDefinitionRegisterProcessor addDefinitionRegisterProcessor() {
        return null;
    }

}
